/**
 *    Copyright 2009 Seth Albnaese
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.aquiferserver.server;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;

/**
 * @author Seth Albanese
 * @version $Id: AquiferServer.java 24 2009-05-11 13:27:21Z salbanese0080@gmail.com $
 */
public class AquiferServer 
{
	protected static final Log logger = LogFactory.getLog( org.aquiferserver.server.AquiferServer.class );
		
	private ChannelPipelineFactory pipelineFactory;
	
	public AquiferServer()
	{
		
	}
	
	public void start( Integer port )
	{
		logger.info( "Starting AquiferServer..." );
		
		ChannelFactory factory  = new NioServerSocketChannelFactory (
				Executors.newCachedThreadPool (),
				Executors.newCachedThreadPool ()
		);
		
		ServerBootstrap bootstrap = new ServerBootstrap ( factory );
		
		bootstrap.setPipelineFactory( pipelineFactory );
		
		bootstrap.setOption( "child.tcpNoDelay", true );
		bootstrap.setOption( "child.keepAlive", true );
			
		bootstrap.bind( new InetSocketAddress( port ) );
		
		logger.info( "Server running on port " + port.toString() );
	}
	
	public void setPipelineFactory( ChannelPipelineFactory f )
	{
		pipelineFactory = f;
	}

}
